﻿using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.IdentityModel.Tokens;
using PublicClass;
using S.Services.SheepMale.Domain.Select;
using S.Services.SheepMale.Infrastructure;
using System.Text;

namespace S.Services.SheepMale.Read.Api.Extensions
{
    /// <summary>
    /// 服务扩展(静态类)
    /// </summary>
    public static class SevicesCollectionExtensions
    {
        /// <summary>
        /// 注册JWT
        /// </summary>
        /// <param name="collection"></param>
        /// <param name="builder"></param>
        public static void AddJWT(this IServiceCollection collection, WebApplicationBuilder builder)
        {
            builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(option =>
            {
                //取出私钥
                var secretByte = Encoding.UTF8.GetBytes(builder.Configuration["Authentication:SecretKey"]);
                option.TokenValidationParameters = new TokenValidationParameters()
                {
                    //验证发布者
                    ValidateIssuer = true,
                    ValidIssuer = builder.Configuration["Authentication:Issuer"],
                    //验证接收者
                    ValidateAudience = true,
                    ValidAudience = builder.Configuration["Authentication:Audience"],
                    //验证是否过期
                    ValidateLifetime = true,
                    //验证私钥
                    IssuerSigningKey = new SymmetricSecurityKey(secretByte)
                };
            });
        }
        /// <summary>
        /// 注册接口
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddServices(this IServiceCollection services)
        {
            services.AddScoped<EntityDbContext>();

            services.AddTransient(typeof(IRepository<>),typeof(BaseRepository<>));

            services.AddTransient(typeof(CountInfo<>));

            services.AddTransient(typeof(PageInfo<>));
            return services;
        }
        /// <summary>
        /// 注册数据库
        /// </summary>
        /// <typeparam name="EntityCoreDbContext"></typeparam>
        /// <param name="services"></param>
        /// <param name="connectionString"></param>
        /// <param name="enableSqlLog"></param>
        /// <returns></returns>
        //public static IServiceCollection AddDbContextServices<EntityCoreDbContext>(this IServiceCollection services, string connectionString, bool enableSqlLog) where EntityCoreDbContext : DbContext
        //{
        //    return services.AddDbContext<EntityCoreDbContext>(buider =>
        //    {
        //        buider.UseSqlServer(connectionString, action =>
        //        {
        //            action.EnableRetryOnFailure(
        //                maxRetryCount: 10,
        //                maxRetryDelay: TimeSpan.FromSeconds(60),
        //                errorNumbersToAdd: null);
        //        }).EnableSensitiveDataLogging();
        //        if (enableSqlLog)
        //        {
        //            buider.UseLoggerFactory(LoggerFactory.Create(configure =>
        //            {
        //                configure.AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
        //            }));
        //        }
        //    });
        //}
    }
}
